package com.jiangjiang.eshop.purchase.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.purchase.constant.PurchaseOrderStatus;
import com.jiangjiang.eshop.purchase.convert.PurchaseOrderConvert;
import com.jiangjiang.eshop.purchase.dao.PurchaseOrderMapper;
import com.jiangjiang.eshop.purchase.domain.PurchaseOrderEntity;
import com.jiangjiang.eshop.purchase.dto.PurchaseOrderDTO;
import com.jiangjiang.eshop.purchase.dto.PurchaseOrderPageQuery;
import com.jiangjiang.eshop.schedule.service.ScheduleService;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderApproveResult;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName PurchaseOrderService
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/20 7:31
 * @Version 1.0
 **/
@Service
public class PurchaseOrderService extends ServiceImpl<PurchaseOrderMapper, PurchaseOrderEntity> {
    @Autowired
    private PurchaseOrderConvert purchaseOrderConvert;
    @Autowired
    private PurchaseOrderItemService purchaseOrderItemService;
    @Autowired
    private ScheduleService scheduleService;


    /**
     * 采购单保存
     *
     * @param dto 采购单数据
     * @return 处理结果
     */
    public Boolean savePurchaseOrder(PurchaseOrderDTO dto) {
        // 保存采购单数据
        PurchaseOrderEntity purchaseOrder = purchaseOrderConvert.convertDTO2Entity(dto);
        this.save(purchaseOrder);
        // 保存采购单明细数据
        purchaseOrderItemService.savePurchaseOrderItems(purchaseOrder.getId(), dto.getPurchaseOrderItemDTOList());
        return true;

    }


    /**
     * 采购单保存
     *
     * @param dto 采购单数据
     * @return 处理结果
     */
    public Boolean updatePurchaseOrder(PurchaseOrderDTO dto) {
        // 保存采购单数据
        PurchaseOrderEntity purchaseOrder = purchaseOrderConvert.convertDTO2Entity(dto);
        this.updateById(purchaseOrder);
        // 删除采购单明细数据
        purchaseOrderItemService.removeByPurchaseOrderId(dto.getId());
        // 保存新的采购单明细数据
        purchaseOrderItemService.savePurchaseOrderItems(purchaseOrder.getId(), dto.getPurchaseOrderItemDTOList());
        return true;

    }


    /**
     * 根据采购单id查询采购单数据
     *
     * @param purchaseOrderId 采购单数据
     * @return 处理结果
     */
    public PurchaseOrderDTO queryById(Long purchaseOrderId) {
        PurchaseOrderDTO purchaseOrder = purchaseOrderConvert.convertEntity2DTO(this.getById(purchaseOrderId));
        // 补充明细数据
        purchaseOrder.setPurchaseOrderItemDTOList(purchaseOrderItemService.queryByPurchaseOrderId(purchaseOrderId));
        return purchaseOrder;

    }

    /**
     * 分页查询采购单数据
     *
     * @param query 采购单数据
     * @return 查询结果
     */
    public Page<PurchaseOrderDTO> queryForPage(PurchaseOrderPageQuery query) {

        Page<PurchaseOrderEntity> page = new Page<>(query.getPageNo(), query.getPageSize(), true);
        LambdaQueryWrapper<PurchaseOrderEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getSupplierId() != null, PurchaseOrderEntity::getSupplierId, query.getSupplierId())
                .eq(query.getStatus() != null, PurchaseOrderEntity::getStatus, query.getStatus())
        ;
        return purchaseOrderConvert.convertPage2DTO(this.getBaseMapper().selectPage(page, wrapper));
    }


    /**
     * 采购单提交审核
     *
     * @param id 采购单id
     * @return 处理结果
     */
    public Boolean submitApprove(Long id) {
        return updatePurchaseOrderStatus(id, PurchaseInputOrderStatus.WAIT_FOR_APPROVE);
    }

    /**
     * 采购单提交审核
     *
     * @param id 采购单id
     * @return 处理结果
     */
    public Boolean approve(Long id,Integer approveResult) {
        if(PurchaseInputOrderApproveResult.REJECTED.equals(approveResult)){
            return updatePurchaseOrderStatus(id, PurchaseOrderStatus.EDITING);

        }
        updatePurchaseOrderStatus(id, PurchaseOrderStatus.WAIT_FOR_APPROVE);
        scheduleService.schedulePurchaseInput(queryById(id));
        return true;
    }

    /**
     * 采购单状态更新
     *
     * @param id     采购单id
     * @param status 状态
     * @return 处理结果
     */
    public Boolean updatePurchaseOrderStatus(Long id, Integer status) {
        LambdaUpdateWrapper<PurchaseOrderEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(PurchaseOrderEntity::getId, id)
                .set(PurchaseOrderEntity::getStatus, status);
        return this.update(wrapper);
    }


}
